package com.example.lotterysystem.service.activitystatus.operater;

import com.example.lotterysystem.mapper.ActivityUserMapper;
import com.example.lotterysystem.pojo.dataobject.ActivityUserDO;
import com.example.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.example.lotterysystem.service.enums.ActivityUserStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
public class UserOperator extends AbstractActivityOperator{

    @Autowired
    private ActivityUserMapper activityUserMapper;

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        List<Long> userIds = convertActivityStatusDTO.getUserId();
        ActivityUserStatusEnum targetStatus = convertActivityStatusDTO.getTargetUserStatus();

        // 检验是否存在为空的情况
        if (activityId == null
                || targetStatus == null
                || CollectionUtils.isEmpty(userIds)) {
            return false;
        }

        // 从 活动人员关联表 中进行查询 活动人员关联表对应的信息
        List<ActivityUserDO> activityUserDOList =
                activityUserMapper.batchSelectByAUIds(activityId,userIds);
        if (CollectionUtils.isEmpty(activityUserDOList)) {
            return false;
        }

        // 遍历所有的人员表，进行查询状态是否和传入的状态一致
        for (ActivityUserDO activityUserDO : activityUserDOList) {
             if (activityUserDO.getStatus().
                     equalsIgnoreCase(targetStatus.name())) {
                 return false;
             }
        }

        return true;
    }

    @Override
    public boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        try {
            Long activityId = convertActivityStatusDTO.getActivityId();
            List<Long> userIds = convertActivityStatusDTO.getUserId();
            ActivityUserStatusEnum targetStatus = convertActivityStatusDTO.getTargetUserStatus();
            activityUserMapper.batchUpdateStatus(activityId,userIds,targetStatus.name());
            return true;
        }catch (Exception e) {
            return false;
        }
    }
}
